<script setup>
import { ref, reactive, onMounted } from 'vue'
import { getPostTopics } from '@/api/post/postManagement.js'

// 批量操作筛选条件
const filters = reactive({
  topicId: null,
  timeRange: null,
  contentKey: '',
  startLikeNum: null,
  endLikeNum: null,
  startCollectNum: null,
  endCollectNum: null,
  startViewNum: null,
  endViewNum: null,
  startWordNum: null,
  endWordNum: null,
  isNameless: null,
  isDie: null
})

// 批量设置
const batchSettings = reactive({
  targetHot: 100,
  affectCount: 10
})

// 预览数据
const previewPosts = ref([])
const showPreview = ref(false)
const topics = ref([])

// 获取话题列表
const getTopics = () => {
  getPostTopics().then((res) => {
    topics.value = res.data
  })
}

// 获取预览数据
const getPreviewPosts = () => {
  // TODO: 调用API获取符合条件的帖子
  showPreview.value = true
}

// 执行批量投流
const executeBatchFlow = () => {
  // TODO: 调用API执行批量投流
}

onMounted(() => {
  getTopics()
})


// 添加抽屉控制
const drawerVisible = ref(false)

// 打开抽屉
const openFilterDrawer = () => {
  drawerVisible.value = true
}

// 应用筛选
const applyFilters = () => {
  drawerVisible.value = false
  getPreviewPosts()
}
</script>

<template>
  <div class="batch-flow">
    <!-- 筛选按钮 -->
    <div class="filter-trigger">
      <el-button type="primary" @click="openFilterDrawer">
        <el-icon><Filter /></el-icon>
        筛选条件
      </el-button>
    </div>

    <!-- 批量设置 -->
    <el-card class="batch-settings">
      <template #header>
        <div class="card-header">
          <span>批量投流设置</span>
        </div>
      </template>
      <el-form label-width="120px">
        <el-form-item label="目标热度值">
          <el-input-number v-model="batchSettings.targetHot" :min="0" />
        </el-form-item>
        <el-form-item label="影响帖子数量">
          <el-input-number v-model="batchSettings.affectCount" :min="1" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getPreviewPosts">预览</el-button>
        </el-form-item>
      </el-form>

    </el-card>

    <!-- 预览区域 -->
    <div v-if="showPreview" class="preview-section">
      <el-card>
        <template #header>
          <div class="card-header">
            <span>预览结果</span>
            <el-button type="primary" @click="executeBatchFlow">确认执行</el-button>
          </div>
        </template>
        <el-table :data="previewPosts" border style="width: 100%">
          <el-table-column prop="title" label="帖子标题" />
          <el-table-column prop="topic" label="话题" />
          <el-table-column prop="author" label="发布者" />
          <el-table-column prop="createTime" label="发布时间" />
          <el-table-column prop="hot" label="当前热度" />
          <el-table-column prop="targetHot" label="目标热度" />
        </el-table>
      </el-card>
    </div>

    <!-- 筛选条件抽屉 -->
    <el-drawer
      v-model="drawerVisible"
      title="筛选条件"
      direction="rtl"
      size="450px"
    >
      <template #default>
<!--        <el-form>-->
<!--          &lt;!&ndash; 将原有的筛选表单移到这里 &ndash;&gt;-->
<!--          <el-form-item label="话题">-->
<!--            <el-select v-model="filters.topicId" placeholder="选择话题" clearable>-->
<!--              <el-option-->
<!--                v-for="item in topics"-->
<!--                :key="item.id"-->
<!--                :label="item.title"-->
<!--                :value="item.id"-->
<!--              />-->
<!--            </el-select>-->
<!--          </el-form-item>-->
<!--          &lt;!&ndash; ... 其他筛选条件 ... &ndash;&gt;-->
<!--        </el-form>-->
        <el-form inline>
          <el-form-item label="话题">
            <el-select v-model="filters.topicId" placeholder="选择话题" clearable>
              <el-option label="全部" value=""></el-option>
              <!-- TODO: 添加话题选项 -->
            </el-select>
          </el-form-item>

          <el-form-item label="发布时间">
            <el-date-picker
                v-model="filters.timeRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
            />
          </el-form-item>

          <el-form-item label="热度范围">
            <el-input-number v-model="filters.minHot" placeholder="最小值" />
            <span style="margin: 0 10px">-</span>
            <el-input-number v-model="filters.maxHot" placeholder="最大值" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="getPosts">查询</el-button>
            <el-button @click="() => {
            Object.assign(filters, {
              topicId: null,
              timeRange: null,
              contentKey: '',
              minHot: null,
              maxHot: null
            })
          }">重置</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <div style="flex: auto">
          <el-button @click="drawerVisible = false">取消</el-button>
          <el-button type="primary" @click="applyFilters">应用</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<style scoped>
.batch-flow {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.filter-trigger {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 8px;
}

.batch-settings,
.preview-section {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

:deep(.el-drawer__body) {
  padding: 20px;
}

:deep(.el-form-item) {
  margin-bottom: 22px;
}

.input-number-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
}

.input-number-wrapper :deep(.el-input-number) {
  width: 120px;
}
</style>